﻿package com.floorplanner.lib3ds
{
	import com.floorplanner.lib3ds.Lib3dsIO;
	
	public class Lib3dsChunk
	{
		public static const NULL_CHUNK:uint = 0x0000;
		public static const M3DMAGIC:uint = 0x4D4D;    /*3DS file*/
		public static const SMAGIC:uint = 0x2D2D;
		public static const LMAGIC:uint = 0x2D3D;
		public static const MLIBMAGIC:uint = 0x3DAA;    /*MLI file*/
		public static const MATMAGIC:uint = 0x3DFF;
		public static const CMAGIC:uint = 0xC23D;    /*PRJ file*/
		public static const M3D_VERSION:uint = 0x0002;
		public static const M3D_KFVERSION:uint = 0x0005;
		public static const COLOR_F:uint = 0x0010;
		public static const COLOR_24:uint = 0x0011;
		public static const LIN_COLOR_24:uint = 0x0012;
		public static const LIN_COLOR_F:uint = 0x0013;
		public static const INT_PERCENTAGE:uint = 0x0030;
		public static const FLOAT_PERCENTAGE:uint = 0x0031;
		public static const MDATA:uint = 0x3D3D;
		public static const MESH_VERSION:uint = 0x3D3E;
		public static const MASTER_SCALE:uint = 0x0100;
		public static const LO_SHADOW_BIAS:uint = 0x1400;
		public static const HI_SHADOW_BIAS:uint = 0x1410;
		public static const SHADOW_MAP_SIZE:uint = 0x1420;
		public static const SHADOW_SAMPLES:uint = 0x1430;
		public static const SHADOW_RANGE:uint = 0x1440;
		public static const SHADOW_FILTER:uint = 0x1450;
		public static const RAY_BIAS:uint = 0x1460;
		public static const O_CONSTS:uint = 0x1500;
		public static const AMBIENT_LIGHT:uint = 0x2100;
		public static const BIT_MAP:uint = 0x1100;
		public static const SOLID_BGND:uint = 0x1200;
		public static const V_GRADIENT:uint = 0x1300;
		public static const USE_BIT_MAP:uint = 0x1101;
		public static const USE_SOLID_BGND:uint = 0x1201;
		public static const USE_V_GRADIENT:uint = 0x1301;
		public static const FOG:uint = 0x2200;
		public static const FOG_BGND:uint = 0x2210;
		public static const LAYER_FOG:uint = 0x2302;
		public static const DISTANCE_CUE:uint = 0x2300;
		public static const DCUE_BGND:uint = 0x2310;
		public static const USE_FOG:uint = 0x2201;
		public static const USE_LAYER_FOG:uint = 0x2303;
		public static const USE_DISTANCE_CUE:uint = 0x2301;
		public static const MAT_ENTRY:uint = 0xAFFF;
		public static const MAT_NAME:uint = 0xA000;
		public static const MAT_AMBIENT:uint = 0xA010;
		public static const MAT_DIFFUSE:uint = 0xA020;
		public static const MAT_SPECULAR:uint = 0xA030;
		public static const MAT_SHININESS:uint = 0xA040;
		public static const MAT_SHIN2PCT:uint = 0xA041;
		public static const MAT_TRANSPARENCY:uint = 0xA050;
		public static const MAT_XPFALL:uint = 0xA052;
		public static const MAT_USE_XPFALL:uint = 0xA240;
		public static const MAT_REFBLUR:uint = 0xA053;
		public static const MAT_SHADING:uint = 0xA100;
		public static const MAT_USE_REFBLUR:uint = 0xA250;
		public static const MAT_SELF_ILLUM:uint = 0xA084;
		public static const MAT_TWO_SIDE:uint = 0xA081;
		public static const MAT_DECAL:uint = 0xA082;
		public static const MAT_ADDITIVE:uint = 0xA083;
		public static const MAT_WIRE:uint = 0xA085;
		public static const MAT_FACEMAP:uint = 0xA088;
		public static const MAT_TRANSFALLOFF_IN:uint = 0xA08A;
		public static const MAT_PHONGSOFT:uint = 0xA08C;
		public static const MAT_WIREABS:uint = 0xA08E;
		public static const MAT_WIRE_SIZE:uint = 0xA087;
		public static const MAT_TEXMAP:uint = 0xA200;
		public static const MAT_SXP_TEXT_DATA:uint = 0xA320;
		public static const MAT_TEXMASK:uint = 0xA33E;
		public static const MAT_SXP_TEXTMASK_DATA:uint = 0xA32A;
		public static const MAT_TEX2MAP:uint = 0xA33A;
		public static const MAT_SXP_TEXT2_DATA:uint = 0xA321;
		public static const MAT_TEX2MASK:uint = 0xA340;
		public static const MAT_SXP_TEXT2MASK_DATA:uint = 0xA32C;
		public static const MAT_OPACMAP:uint = 0xA210;
		public static const MAT_SXP_OPAC_DATA:uint = 0xA322;
		public static const MAT_OPACMASK:uint = 0xA342;
		public static const MAT_SXP_OPACMASK_DATA:uint = 0xA32E;
		public static const MAT_BUMPMAP:uint = 0xA230;
		public static const MAT_SXP_BUMP_DATA:uint = 0xA324;
		public static const MAT_BUMPMASK:uint = 0xA344;
		public static const MAT_SXP_BUMPMASK_DATA:uint = 0xA330;
		public static const MAT_SPECMAP:uint = 0xA204;
		public static const MAT_SXP_SPEC_DATA:uint = 0xA325;
		public static const MAT_SPECMASK:uint = 0xA348;
		public static const MAT_SXP_SPECMASK_DATA:uint = 0xA332;
		public static const MAT_SHINMAP:uint = 0xA33C;
		public static const MAT_SXP_SHIN_DATA:uint = 0xA326;
		public static const MAT_SHINMASK:uint = 0xA346;
		public static const MAT_SXP_SHINMASK_DATA:uint = 0xA334;
		public static const MAT_SELFIMAP:uint = 0xA33D;
		public static const MAT_SXP_SELFI_DATA:uint = 0xA328;
		public static const MAT_SELFIMASK:uint = 0xA34A;
		public static const MAT_SXP_SELFIMASK_DATA:uint = 0xA336;
		public static const MAT_REFLMAP:uint = 0xA220;
		public static const MAT_REFLMASK:uint = 0xA34C;
		public static const MAT_SXP_REFLMASK_DATA:uint = 0xA338;
		public static const MAT_ACUBIC:uint = 0xA310;
		public static const MAT_MAPNAME:uint = 0xA300;
		public static const MAT_MAP_TILING:uint = 0xA351;
		public static const MAT_MAP_TEXBLUR:uint = 0xA353;
		public static const MAT_MAP_USCALE:uint = 0xA354;
		public static const MAT_MAP_VSCALE:uint = 0xA356;
		public static const MAT_MAP_UOFFSET:uint = 0xA358;
		public static const MAT_MAP_VOFFSET:uint = 0xA35A;
		public static const MAT_MAP_ANG:uint = 0xA35C;
		public static const MAT_MAP_COL1:uint = 0xA360;
		public static const MAT_MAP_COL2:uint = 0xA362;
		public static const MAT_MAP_RCOL:uint = 0xA364;
		public static const MAT_MAP_GCOL:uint = 0xA366;
		public static const MAT_MAP_BCOL:uint = 0xA368;
		public static const NAMED_OBJECT:uint = 0x4000;
		public static const N_DIRECT_LIGHT:uint = 0x4600;
		public static const DL_OFF:uint = 0x4620;
		public static const DL_OUTER_RANGE:uint = 0x465A;
		public static const DL_INNER_RANGE:uint = 0x4659;
		public static const DL_MULTIPLIER:uint = 0x465B;
		public static const DL_EXCLUDE:uint = 0x4654;
		public static const DL_ATTENUATE:uint = 0x4625;
		public static const DL_SPOTLIGHT:uint = 0x4610;
		public static const DL_SPOT_ROLL:uint = 0x4656;
		public static const DL_SHADOWED:uint = 0x4630;
		public static const DL_LOCAL_SHADOW2:uint = 0x4641;
		public static const DL_SEE_CONE:uint = 0x4650;
		public static const DL_SPOT_RECTANGULAR:uint = 0x4651;
		public static const DL_SPOT_ASPECT:uint = 0x4657;
		public static const DL_SPOT_PROJECTOR:uint = 0x4653;
		public static const DL_SPOT_OVERSHOOT:uint = 0x4652;
		public static const DL_RAY_BIAS:uint = 0x4658;
		public static const DL_RAYSHAD:uint = 0x4627;
		public static const N_CAMERA:uint = 0x4700;
		public static const CAM_SEE_CONE:uint = 0x4710;
		public static const CAM_RANGES:uint = 0x4720;
		public static const OBJ_HIDDEN:uint = 0x4010;
		public static const OBJ_VIS_LOFTER:uint = 0x4011;
		public static const OBJ_DOESNT_CAST:uint = 0x4012;
		public static const OBJ_DONT_RECVSHADOW:uint = 0x4017;
		public static const OBJ_MATTE:uint = 0x4013;
		public static const OBJ_FAST:uint = 0x4014;
		public static const OBJ_PROCEDURAL:uint = 0x4015;
		public static const OBJ_FROZEN:uint = 0x4016;
		public static const N_TRI_OBJECT:uint = 0x4100;
		public static const POINT_ARRAY:uint = 0x4110;
		public static const POINT_FLAG_ARRAY:uint = 0x4111;
		public static const FACE_ARRAY:uint = 0x4120;
		public static const MSH_MAT_GROUP:uint = 0x4130;
		public static const SMOOTH_GROUP:uint = 0x4150;
		public static const MSH_BOXMAP:uint = 0x4190;
		public static const TEX_VERTS:uint = 0x4140;
		public static const MESH_MATRIX:uint = 0x4160;
		public static const MESH_COLOR:uint = 0x4165;
		public static const MESH_TEXTURE_INFO:uint = 0x4170;
		public static const KFDATA:uint = 0xB000;
		public static const KFHDR:uint = 0xB00A;
		public static const KFSEG:uint = 0xB008;
		public static const KFCURTIME:uint = 0xB009;
		public static const AMBIENT_NODE_TAG:uint = 0xB001;
		public static const OBJECT_NODE_TAG:uint = 0xB002;
		public static const CAMERA_NODE_TAG:uint = 0xB003;
		public static const TARGET_NODE_TAG:uint = 0xB004;
		public static const LIGHT_NODE_TAG:uint = 0xB005;
		public static const L_TARGET_NODE_TAG:uint = 0xB006;
		public static const SPOTLIGHT_NODE_TAG:uint = 0xB007;
		public static const NODE_ID:uint = 0xB030;
		public static const NODE_HDR:uint = 0xB010;
		public static const PIVOT:uint = 0xB013;
		public static const INSTANCE_NAME:uint = 0xB011;
		public static const MORPH_SMOOTH:uint = 0xB015;
		public static const BOUNDBOX:uint = 0xB014;
		public static const POS_TRACK_TAG:uint = 0xB020;
		public static const COL_TRACK_TAG:uint = 0xB025;
		public static const ROT_TRACK_TAG:uint = 0xB021;
		public static const SCL_TRACK_TAG:uint = 0xB022;
		public static const MORPH_TRACK_TAG:uint = 0xB026;
		public static const FOV_TRACK_TAG:uint = 0xB023;
		public static const ROLL_TRACK_TAG:uint = 0xB024;
		public static const HOT_TRACK_TAG:uint = 0xB027;
		public static const FALL_TRACK_TAG:uint = 0xB028;
		public static const HIDE_TRACK_TAG:uint = 0xB029;
		public static const POLY_2D:uint = 0x5000;
		public static const SHAPE_OK:uint = 0x5010;
		public static const SHAPE_NOT_OK:uint = 0x5011;
		public static const SHAPE_HOOK:uint = 0x5020;
		public static const PATH_3D:uint = 0x6000;
		public static const PATH_MATRIX:uint = 0x6005;
		public static const SHAPE_2D:uint = 0x6010;
		public static const M_SCALE:uint = 0x6020;
		public static const M_TWIST:uint = 0x6030;
		public static const M_TEETER:uint = 0x6040;
		public static const M_FIT:uint = 0x6050;
		public static const M_BEVEL:uint = 0x6060;
		public static const XZ_CURVE:uint = 0x6070;
		public static const YZ_CURVE:uint = 0x6080;
		public static const INTERPCT:uint = 0x6090;
		public static const DEFORM_LIMIT:uint = 0x60A0;
		public static const USE_CONTOUR:uint = 0x6100;
		public static const USE_TWEEN:uint = 0x6110;
		public static const USE_SCALE:uint = 0x6120;
		public static const USE_TWIST:uint = 0x6130;
		public static const USE_TEETER:uint = 0x6140;
		public static const USE_FIT:uint = 0x6150;
		public static const USE_BEVEL:uint = 0x6160;
		public static const DEFAULT_VIEW:uint = 0x3000;
		public static const VIEW_TOP:uint = 0x3010;
		public static const VIEW_BOTTOM:uint = 0x3020;
		public static const VIEW_LEFT:uint = 0x3030;
		public static const VIEW_RIGHT:uint = 0x3040;
		public static const VIEW_FRONT:uint = 0x3050;
		public static const VIEW_BACK:uint = 0x3060;
		public static const VIEW_USER:uint = 0x3070;
		public static const VIEW_CAMERA:uint = 0x3080;
		public static const VIEW_WINDOW:uint = 0x3090;
		public static const VIEWPORT_LAYOUT_OLD:uint = 0x7000;
		public static const VIEWPORT_DATA_OLD:uint = 0x7010;
		public static const VIEWPORT_LAYOUT:uint = 0x7001;
		public static const VIEWPORT_DATA:uint = 0x7011;
		public static const VIEWPORT_DATA_3:uint = 0x7012;
		public static const VIEWPORT_SIZE:uint = 0x7020;
		public static const NETWORK_VIEW:uint = 0x7030;

		public var cur:uint;
		public var chunk:Number;
		public var size:uint;
		public var end:uint;
		
		public function Lib3dsChunk()
		{
			
		}
				
		/**
		 * read next chunk.
		 * 
		 * @param	io
		 * @return
		 */
		public function next( io:Lib3dsIO ):uint
		{
			if( this.cur >= this.end )
			{
				return 0;
			}
			
			try
			{
				io.position = this.cur;
				
				var d:Lib3dsChunk = new Lib3dsChunk();
				d.chunk = io.readWord();
				d.size = io.readDWord();
				this.cur += d.size;
				return d.chunk;
			}
			catch(e:Error)
			{
			}
			return 0;
		}
		
		/**
		 * 
		 * @param	io
		 * @return
		 */
		public static function read( io:Lib3dsIO ):Lib3dsChunk
		{
			var c:Lib3dsChunk = new Lib3dsChunk();
			c.cur = io.position;
			c.chunk = io.readWord();
			c.size = io.readDWord();
			c.end = c.cur + c.size;
			c.cur += 6;
			return c;
		}
		
		/**
		 * set IO to chunk's end.
		 * 
		 * @param	io
		 */
		public function readEnd( io:Lib3dsIO ):void 
		{
			io.position = this.end;
		}
		
		/**
		 * reset IO.
		 * 
		 * @param	io
		 */
		public function reset( io:Lib3dsIO ):void
		{
			io.position -= 6;
		}
	}
}
